package linkedList;

import java.util.Stack;

/**
 * @author leiyuee
 * @ClassName linkedList.SingleLinkedList 单链表
 * @date 2021/11/3 9:20
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);
        //显示
        singleLinkedList.list();

        singleLinkedList.reverset(hero1);


//        linkedList.HeroNode newHeroNode = new linkedList.HeroNode(2, "卢俊义", "玉麒麟--");
//        singleLinkedList.update(newHeroNode);
        singleLinkedList.list();
        singleLinkedList.del(1);
    }
}

/**
 * 1.定义HERONode
 */
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next; //下一节点

    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "linkedList.HeroNode[no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
    }
}

/**
 * 定义SinglelinkedList 管理我们的英雄
 */
class SingleLinkedList {


    private HeroNode head = new HeroNode(0, "", "");

    /**
     * 添加：
     * 1、添加到最后,原最后节点指向新节点
     */
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;

        }
        temp.next = heroNode;
    }

    /**
     * 根据排名插入
     */
    public void addByOrder(HeroNode heroNode) {

        HeroNode temp = head;
        boolean flag = false;//默认添加的编号不存在
        while (true) {
            if (temp.next == null) {//temp 是最后一个
                break;
            }
            if (temp.next.no > heroNode.no) {//temp的下一个
                break;
            } else if (temp.next.no == heroNode.no) { //已存在

                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("英雄编号已存在");
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    //修改节点信息，需要根据no编号修改
    public void update(HeroNode heroNode) {

        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        HeroNode temp = head.next;
        boolean flag = false;

        while (true) {
            if (temp == null) {
                break;//遍历完了
            }

            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }

            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.println("没有找到节点");
        }
    }

    //删除节点
    public void del(int no) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) { //找到前一个节点
                flag = true;
                break;
            }
            temp = temp.next;//temp 后移
        }

        if (flag) {
            temp.next = temp.next.next; //删除了
        } else {
            System.out.println("要删除的节点不存在");
        }
    }

    //列表（遍历）
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }


    //面试题
    //节点数
    public int getLength(HeroNode head) {
        if (head.next == null) {
            return 0;
        }
        int length = 0;
        HeroNode temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    //倒数第K个节点
    public HeroNode getIndexNode(HeroNode head, int index) {
        if (head.next == null) {
            return null;
        }
        int size = getLength(head);
        if (index < 1 || index > size) {
            return null;
        }
        HeroNode temp = head.next;
        for (int i = 0; i < size - index; i++) {
            temp = temp.next;
        }
        return temp;
    }

    //反转
    public void reverset(HeroNode head) {
        if (head.next == null || head.next.next == null) {
            return;
        }
        HeroNode temp = head.next;//第一个
        HeroNode next = null;
        HeroNode reverseHead = new HeroNode(0, "", "");
        while (temp != null) {
            next = temp.next;
            //下面两步是将新链表的最前端 拼接到temp，然后temp 放到最前端
            temp.next = reverseHead.next;
            reverseHead.next = temp;

            temp = next;
        }
        head.next = reverseHead.next;
    }

    //从头到尾打印链表→利用栈stack:特点是 先进后出
    public void reversePrint(HeroNode head){
        if (head.next == null) {
            return;
        }
        Stack<HeroNode> stack = new Stack<>();
        HeroNode temp = head;
        while (temp!=null){
            stack.push(temp);
            temp= temp.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());//将栈顶的数据去除
        }

    }







}